package com.sss.springcloud.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sss.springcloud.common.Result;
import com.sss.springcloud.entities.Account;
import com.sss.springcloud.service.IAccountService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sss
 * @since 2020-03-13
 */
@RestController
@RequestMapping
public class AccountController {
    
    @Resource
    private IAccountService accountService;
    
    @PostMapping("account/decrease")
    public Result decrease(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money){

        Account account = accountService.list().get(0);
//         try {
//                     TimeUnit.SECONDS.sleep(10);
//                 } catch (InterruptedException e) {
//                     e.printStackTrace();
//                 }
        int age=10/0;
        Account newAccount = new Account();
      //  BigDecimal newTotal = account.getTotal().subtract(money);
        BigDecimal newUsed = account.getUsed().add(money);
        BigDecimal newResidue = account.getResidue().subtract(money);
        newAccount.setResidue(newResidue);
        newAccount.setUsed(newUsed);
        accountService.update(newAccount,new UpdateWrapper<Account>().eq("user_id",userId));
        return Result.succes(200,"额度扣减成功");
    }

}

